<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
	include_once("include/config.php");
	include_once("include/auth.lib.php");
?>

<html>
	<head>
		<title><?php echo TITLE ?></title>
		<link href="lib/jqueryui/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css"/>
		<link href="judoinvorio.css" rel="stylesheet" type="text/css"/>
		
		<script src="lib/jquery.js"></script>
		<script src="lib/jquery-ui.min.js"></script>
		
		<link rel="stylesheet" type="text/css" href="flexigrid/css/flexigrid/flexigrid.css">
		<script type="text/javascript" src="flexigrid/flexigrid.js"></script>
		
		<script>
			$(document).ready(function() {
				$("input:button, input:reset, input:submit").button();
			});
		</script>		
  
	</head>
	<body>
	
<?php

	list($status, $user) = auth_get_status();

	if ($status == AUTH_LOGGED) {
	
	echo "
	
	<div id='buttonContainer'>
		<form action='logout.php'>
			<input id='btnLogOut' type='submit' value='Log Out' />
		</form>
	</div>
	
	<div id='title'>Palestra: ".$user["name"]."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";//."(".$user["id_palestra"].")</div>";
?>

	<form>
		<table id="add-atleta" width="500">
			<tr>
				<td><b>Cognome e nome</b></td><td><input type="text" name="COGNOMENOME" id="COGNOMENOME"></td>
			</tr><tr>
				<td><b>Categoria</b></td><td colspan=2>
					<select name="CATEGORIA" id="CATEGORIA">
						<option value="BA">Bambini</option>
						<option value="FA">Fanciulli</option>
						<option value="RAM">Ragazzi M</option>
						<option value="RAF">Ragazzi F</option>					
						<option value="ESAM">Esordienti A M</option>
						<option value="ESAF">Esordienti A F</option>
						<option value="ESBM">Esordienti B M</option>
						<option value="ESBF">Esordienti B F</option>
					</select>
				</td>
			</tr><tr>
				<td><b>Peso</b></td><td colspan=2><input type="text" name="PESO" id="PESO" style="width:50px"> Kg</td>
			</tr><tr>
				<td colspan=2>&nbsp;</td>
			</tr><tr>
				<td><input align="center" type="button" value="Aggiungi" onclick="addAtleta()"></td><td><input align="center" type="reset" value="Cancella"></td>
			</tr>
		</table>
	</form>
	
	<br/><br/>
	
	<div id="dialog-confirm" title="Conferma" style="display:none">
		<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Confermi l'eliminazione dell'atleta selezionato?</p>
	</div>
	
	<div id="grid-atleti" class="box-centered">
		<table id="flex1" style="display:none"></table>
	</div>
	
	<div id="dialog-validation" title="Errore" style="display:none;color:red">
		<p><span style="float:left; margin:0 7px 20px 0;"></span>Tutti i campi sono obbligatori</p>
	</div>

	<div id="dialog-max" title="Errore" style="display:none;color:red">
		<p><span style="float:left; margin:0 7px 20px 0;"></span>Raggiunto il numero massimo di iscrizioni</p>
	</div>	
	
	<script type="text/javascript">
	
		function addAtleta() {
			
			if ($('#COGNOMENOME').val() == '' || $('#PESO').val() == '') {
				$( "#dialog-validation" ).dialog({
					resizable: false,
					modal: true,
					buttons: {
						"Ok": function() {
							$(this).dialog( "close" );
						}
					}
				});
				return false;			
			}
		
			$.ajax({
			  url: "add.php",
			  type: 'POST',
			  data: {
				'COGNOMENOME': $('#COGNOMENOME').val(),
				'CATEGORIA': $('#CATEGORIA').val(),
				'PESO': $('#PESO').val()
			  },
			  success: function(msg) {
				if (msg == 'max-error') {
					$( "#dialog-max" ).dialog({
						resizable: false,
						modal: true,
						buttons: {
							"Ok": function() {
								$(this).dialog( "close" );
							}
						}
					});				
				}
				$('#flex1').flexReload();
			  },
			  error: function() {
				alert('error');
			  }
			});
		}
		
		
		function doCommand(com, grid) {
			$('.trSelected', grid).each(function() {
				var id = $(this).attr('id');
				id = id.substring(id.lastIndexOf('row') + 3);
				
				$( "#dialog-confirm" ).dialog({
					resizable: false,
					height:200,
					modal: true,
					buttons: {
						"Si": function() {
							$.ajax({
							  url: "delete.php",
							  type: 'POST',
							  data: {
								'ID': id
							  },
							  success: function() {
								$('#flex1').flexReload();
							  },
							  error: function() {
								alert('error');
							  }
							});
							$( this ).dialog( "close" );
						},
						"No": function() {
							$( this ).dialog( "close" );
						}
					}
				});
			});
		}		
			
		$("#flex1").flexigrid(
			{
				url: 'list.php',
				dataType: 'json',
				colModel : [
					{display: 'ID', name: 'id', sortable: true, align: 'center', hide: true},
					{display: 'ID palestra', name: 'id_palestra', width: 100, sortable: true, align: 'center', hide: true},
					{display: 'Palestra', name: 'nome_palestra', width: 100, sortable: true, align: 'left'},
					{display: 'Cognome e nome', name: 'cognomenome', width: 300, sortable: true, align: 'left'},
					{display: 'Categoria', name: 'categoria', width : 250, sortable: true, align: 'left'},
					{display: 'Peso', name: 'peso', width : 50, sortable: true, align: 'right'}
				],
				buttons : [
					{name: 'Elimina atleta selezionato', bclass: 'delete', onpress : doCommand},
					{separator: true}
				],
				sortname: "cognomenome",
				sortorder: "asc",
				usepager: false,
				title: 'Atleti',
				useRp: false,
				rp: 15,
				showTableToggleBtn: false,
				singleSelect: true
			}
		);


		//This function adds paramaters to the post of flexigrid. You can add a verification as well by return to false if you don't want flexigrid to submit			
		function addFormData() {
			
			//passing a form object to serializeArray will get the valid data from all the objects, but, if the you pass a non-form object, you have to specify the input elements that the data will come from
			var dt = $('#sform').serializeArray();
			$("#flex1").flexOptions({params: dt});
			return true;
		}
			
		$('#sform').submit(
			function () {
					$('#flex1').flexOptions({newp: 1}).flexReload();
					return false;
				}
		);							
	</script>

<?php	
	} else	{
?>
	<div style='text-align:center' class='ui-state-error'>ACCESSO NEGATO</div>
	<br/><br/>
	<form action='index.php'>
		<input id="btnLogOut" type="submit" value="Home" />
	</form>

<?php	
	}
?>	

	</body>
</html>
